<template>
	<div style="display: flex;padding: 10px;height: 100%;">
		<el-card shadow="hover" class="layout-padding-auto" style="width: 200px;flex: none;margin-right: 10px;height: 100%;">
			<el-tree
				style="max-width: 600px"
				highlight-current
				:current-node-key="state.defaultChecked"
				node-key="cname"
				:data="state.notiklist"
				:props="defaultProps"
				@node-click="handleNodeClick"
			/>
		</el-card>
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search mb15">
				<el-button size="default" type="success" v-auth="'/group/notik/add'"  @click="onOpenAddCombo('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增
				</el-button>
				<el-button size="default" type="danger" v-auth="'/group/notik/del'"  @click="alldel()">
					<el-icon>
						<ele-Delete />
					</el-icon>
					全部删除
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column prop="name" label="标题名" show-overflow-tooltip></el-table-column>
				<el-table-column prop="value" label="对应值" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button size="small" text type="primary" v-auth="'/group/notik/del'"
							@click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<addDialog ref="addDialogref" @refresh="getTableData()" />
	</div>
</template>

<script setup name="combo">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { notikApi } from '/@/api/notik/index';
import { platformApi } from '/@/api/platform/index';
import commonFunction from '/@/utils/commonFunction';
// 定义变量内容
const { dateFormatYMDHMS } = commonFunction();
// 引入组件
const addDialog = defineAsyncComponent(() => import('./dialog.vue'));
const defaultProps = {
	children: 'children',
	label: 'cname',
}
// 定义变量内容
const addDialogref = ref();

const state = reactive({
	paltformData: {
		title: '',
		isShowDialog: false,
		paltforms: [],
		loading: false
	},
	notiklist:[],
	defaultChecked:'',
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			cname: '',
		},
	},
});
const handleNodeClick = (data) => {
	state.tableData.param.cname = data.cname;
	getTableData();
}
// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true
	notikApi().index({cname:state.tableData.param.cname}).then(res => {
		state.tableData.data = res.data;
		state.tableData.loading = false;
	})
};

//获取notik列表
const getNotikList = () => {
	notikApi().list().then(res => {
		state.notiklist = res.data;
		state.tableData.param.cname = res.data[0].cname;
		state.defaultChecked = res.data[0].cname;
		getTableData();
	})
}

// 打开新增弹窗
const onOpenAddCombo = () => {
	addDialogref.value.openDialog(state.tableData.param.cname);
};
const alldel = () => {
	ElMessageBox.confirm(`此操作将全部删除：“${state.tableData.param.cname}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			notikApi().del({ cname: state.tableData.param.cname}).then(res => {
				getTableData();
				ElMessage.success('删除成功');
			})
		})
		.catch(() => { });
}
// 删除
const onRowDel = (row) => {
	ElMessageBox.confirm(`此操作将永久删除：“${row.name}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			notikApi().del({ cname: state.tableData.param.cname,field:row.name }).then(res => {
				getTableData();
				ElMessage.success('删除成功');
			})
		})
		.catch(() => { });
};
const seachClick = () => {
	onHandleCurrentChange(1)
}
// // 分页改变
// const onHandleSizeChange = (val) => {
// 	state.tableData.param.limit = val;
// 	getTableData();
// };
// // 分页改变
// const onHandleCurrentChange = (val) => {
// 	state.tableData.param.page = val;
// 	getTableData();
// };
// 页面加载时
onMounted(() => {
	getNotikList()
});
</script>
